<?php
App::uses('AppController', 'Controller');
/**
 * Clients Controller
 *
 * @property Client $Client
 */
class ClientsController extends AppController {

	public $components = array('Paginator');

	public function beforeFilter() {
		parent::beforeFilter();
	
		//Grant access to all the methods in this controller
		$this->Auth->allow('historial','edit');
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Client->recursive = 0;
		$this->set('clients', $this->paginate());

	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function historial($id = null) {
		$this->Client->id = $id;
		$group = $this->Auth->user('Group');
		if($group['id'] != 1){
			if (!$this->Client->exists()) {
				//throw new NotFoundException(__('Cliente no encontrado'));
				$this->Session->setFlash(__('Cliente no existe'));
				return $this->redirect($this->Auth->redirect());
			}
			$client = $this->Auth->user('Client');
			
			if ( $client['id'] != $id ) {
				//$this->Session->setFlash(__('ACCESSO DENEGADO '));
				return $this->redirect($this->Auth->redirect());
			}
		}
		$this->Client->recursive = 2;
		$this->set('client', $this->Client->read(null, $id));
	}


/**
 * Historial method
 *
 */
	public function view($id = null) {
		$this->Client->id = $id;
		if (!$this->Client->exists()) {
			//throw new NotFoundException(__('Cliente no encontrado'));
			$this->Session->setFlash(__('Cliente no existe'), 'default', array(), 'good');
			return $this->redirect($this->Auth->redirect());
		}
		$this->set('client', $this->Client->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Client->create();
			if ($this->Client->save($this->request->data)) {
				$this->Session->setFlash(__('El nuevo cliente se ha guardado correctamente'), 'default', array(), 'good');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El nuevo cliente no se ha guardado. Favor intente de nuevo.'), 'default', array(), 'bad');
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Client->id = $id;
		$group = $this->Auth->user('Group');
		if (!$this->Client->exists()) {
			throw new NotFoundException(__('Invalid client'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Client->save($this->request->data)) {
				$this->Session->setFlash(__('Datos modificados correctamente!'), 'default', array(), 'good');
				if($group['id'] != 3)
					$this->redirect(array('action' => 'index'));
				else
					$this->redirect(array('action' => 'historial',$id));
			} else {
				$this->Session->setFlash(__('Los datos del cliente no pudieron ser modificados. Favor intente de nuevo.'), 'default', array(), 'bad');
			}
		} else {
			
			$this->request->data = $this->Client->read(null, $id);
			$this->set('group',$group['id']);
		}
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Client->id = $id;
		if (!$this->Client->exists()) {
			throw new NotFoundException(__('Invalid client'));
		}
		if ($this->Client->delete()) {
			$this->Session->setFlash(__('Cliente borrado correctamente!'), 'default', array(), 'good');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Cliente no ha sido borrado correctamente. Intente de nuevo.'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}
	
	
	
	public function search() {

		if($this->request->query){
			$nombre = $this->request->query['nombre'];
			$cedula = $this->request->query['cedula'];
		}

		if($cedula != ''){
				$this->Paginator->settings = array(
			        'conditions' => array('Client.cedula' => $cedula));
				$this->set('resultados',$this->Paginator->paginate('Client'));
				//$this->set('resultados',$this->Client->find('all',array('conditions'=>array('cedula'=>$cedula))));
					/*$this->set('resultados',$this->Client->find('all',
								array('conditions' => array('OR'=>
									array('nombre LIKE' => '%'.$nombre.'%',
									'cedula' => $cedula)
							))));*/
		}	
		else if($nombre != ''){
			$this->Paginator->settings = array('conditions' => array('Client.nombre LIKE'=>'%'.$nombre.'%'));
			$resultados = $this->Paginator->paginate('Client');
			$this->set('resultados',$resultados);
			//$this->set('resultados',$this->Client->find('all',array('conditions'=>array('nombre LIKE'=>'%'.$nombre.'%'))));
		} else {
			$this->set('resultados',null);	
		}
	} 
	
	
}
